package elicecn.json;

import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.longtop.tpp.provide.service.ITicketServiceStub.ArrayOfFlightBean;
import com.longtop.tpp.provide.service.ITicketServiceStub.FlightBean;
import com.longtop.tpp.provide.service.ITicketServiceStub.TktResponse;

public class ParseTktResponse {
	private static final DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
	private static final DateFormat df2 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static final DateFormat df3 = new SimpleDateFormat("HH:mm");

	public static Map parse(TktResponse o) {
		Map m = new LinkedHashMap();
		m.put("respCode", o.getRespCode());
		m.put("respMsg", o.getRespMsg());
		m.put("flightLst", parse(o.getFlightLst()));
		return m;
	}

	private static List parse(ArrayOfFlightBean o) {
		List list = new ArrayList();
		FlightBean[] array = o.getFlightBean();
		for (FlightBean b : array) {
			list.add(parse(b));
		}
		return list;
	}

	private static Map parse(FlightBean o) {
		Map m = new LinkedHashMap();
		Method[] methods = FlightBean.class.getMethods();
		for (Method method : methods) {
			String methodName = method.getName();
			if (methodName.startsWith("get")) {
				try {
					Object obj = method.invoke(o, null);
					if (obj instanceof CharSequence) {// 字符串
						String name = convertMethodName(methodName, "get");
						m.put(name, obj);
					}else if(obj instanceof Number){//数字
						String name = convertMethodName(methodName, "get");
						m.put(name, obj);
					}else {
						System.out.println(methodName +" : "+ obj.getClass().getName());
					}
				} catch (Exception e) {
					// e.printStackTrace();
				}
			}
		}
		return m;
	}

	private static String convertMethodName(String methodName, String string) {
		return new String(new char[] { methodName.charAt(string.length()) })
				.toLowerCase() + methodName.substring(string.length() + 1);
	}

}
